<?php

/** libraries
 * http://docs.joomla.org/JToolBarHelper
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

class bgViewbg extends JView
{
    public $uscm;
    public $usfm;
    public $data;
    public $area;
    public $pref;
    public $uscl;
    public $pacm;
    public $actm;
    
    public $aut;
    // Overwriting JView display method
    function display($tpl = null)
    {
        $this->aut = new bgAuthorize();

        $model = $this->getModel();
        $this->uscm = $model->getAllUscm();
        
        $_view =  JRequest::getVar('disp', 'center');
        $_task = JRequest::getVar('task', 'display');
        $year = JRequest::getVar('year') == '' ? date('Y') : JRequest::getVar('year');
        $month = JRequest::getVar('month') == '' ? date('m') : JRequest::getVar('month');
        $ocba = JRequest::getVar('ocba');
        switch($_task){
            case 'form':
                           
                JToolBarHelper::title('利用者人数入力', 'stats.png');
                JToolBarHelper::save('save', '登録');
                JToolBarHelper::custom('back','back','戻る','戻る',false);        
                
                if(in_array('11', explode(',',$this->aut->getUserGroup()))){
                    $ocba = $model->getUserOcba($this->aut->getUserID());
                    if(substr($ocba, 0, 1) == 0){
                        $_view = 'center';
                    } else {
                        $_view = 'club';
                    }
                }
                switch($_view){
                    case 'club':
                        $this->pacm = $model->getAllPacm();
                        $this->actm = $model->getAllActm();
                        if(!empty($ocba)){
                            $this->data = $model->getUscl($month, $year, $ocba);
                        } 
                        $this->setLayout('a00840');
                        break;    
                    case 'center':
                        $this->usfm = $model->getUsfmByOcba($ocba);
                        if(!empty($ocba)){
                            $this->data = $model->getUsce($month, $year, $ocba);
                        } 
                    default:
                        $this->setLayout('a00830');
                        break;
                                            
                }
                break;
            case 'display': 
            default:
                $this->groupUser = $this->aut->getUserGroup();
                $doc = JFactory::getDocument();
                $doc->addScript('https://www.google.com/jsapi');
                $doc->addScriptDeclaration("google.load('visualization', '1', {packages: ['corechart']});");
                $doc->addScriptDeclaration("google.load('visualization', '1', {packages: ['table']});");
                $this->usfm = $model->getAllUsfm();
                $this->area = $model->getArea();
                $this->pref = $model->getPrefs();
                JToolBarHelper::title('活動人数集計', 'stats.png');
                JToolBarHelper::custom('print', 'print.png', '印刷', '印刷',false);
                JToolBarHelper::custom('char', 'stats.png', 'グラフ', 'グラフ',false);
                JToolBarHelper::custom('export', 'export.png', 'CSV出力', 'CSV出力',false);
                $this->setLayout('a00800');
                break;    
        }



        parent::display();
    }
}
